package com.bjsxt.homework5;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintLetter {

    private Lock lock = new ReentrantLock();

    private Condition a = lock.newCondition();
    private Condition b = lock.newCondition();
    private Condition c = lock.newCondition();

    public void printA() {
        while (true) {
            lock.lock();
            b.signalAll();
            try {
                System.out.println("a");
                try {
                    a.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } finally {
                lock.unlock();
            }
        }
    }

    public void printB() {
        while (true) {
            lock.lock();
            c.signalAll();
            try {
                System.out.println("b");
                try {
                    b.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } finally {
                lock.unlock();
            }
        }
    }

    public void printC() {
        while (true) {
            lock.lock();
            a.signalAll();
            try {
                System.out.println("c");
                try {
                    c.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
